﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnTheWay.Functions;
using Models;
using OnTheWay.Interfaces;
using LocalService;
using System.Windows.Media;
using System.Windows;
using System.ComponentModel;
using OnTheWay.Exceptions;
using OnTheWay.Enums;
using FileService;

namespace PcBackgroundMgr.Functions
{
    [DesignTimeVisible(false)]
    public class ExhibitModelCtrl : FunctionCtrlModel<Exhibit>
    {
        protected string mType = "展示";

        public ExhibitModelCtrl()
            : base(LocalFactory.ExhibitLocal)
        {

        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }

        protected override void OnInitializeCheckItems(Type type)
        {
            this.AddCheck("Name", "名称");
            this.AddCheck("TypeName", "类型");
            this.AddCheck("Description", "详细描述");
            this.AddCheck("Author", "作者");
            this.AddCheck("MooMaUrl", "封面图片");
        }

        public override Exhibit OnInitializeModel(Exhibit model)
        {
            if (model == null)
            {
                model = new Exhibit();
                this.Title = "添加" + mType;
                this.TextConfirm = "保存并添加内容";
                if (mType == "视频")
                {
                    this.TextConfirm = "保存并添加" + mType;
                }
            }
            else
            {
                this.Title = "编辑" + mType;
                this.mExhibitType = new ExhibitType(model);
                this.MooMaImage = FileFactory.GetUrl(model.MooMaUrl, FolderEnum.IMAGE);
            }
            this.StartLoadingTypes();
            return model;
        }

        protected override void OnModelAdded(Exhibit model)
        {
            DispatcherInvokeSafety(() =>
            {
                this.Title = "编辑" + mType;
                this.TextConfirm = "确定";
                this.OperateMode = OperateModeEnum.Modify;
                if (mActionAdded != null)
                {
                    mActionAdded(model);
                }
            });
        }

        protected void StartLoadingTypes()
        {
            PostTask(() =>
            {
                if (Model == null)
                {
                    return;
                }
                try
                {
                    mltExhibitType = LocalFactory.ExhibitTypeLocal.GetListWhere("", "", "");
                    DispatchPropertyNotifyChanged("ExhibitTypes");
                    if (mltExhibitType.Count == 0)
                    {
                        DispatchMessageStatus = MessageStatusEnum.Warning;
                        DispatchMessage = "还没有添加任何类型，请先添加基本类型。";
                    }
                    foreach (var item in mltExhibitType)
                    {
                        if (item.ID == Model.TypeID)
                        {
                            ExhibitType = item;
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    DispatchMessageStatus = MessageStatusEnum.Error;
                    DispatchMessage = "加载类型失败：" + ExceptionHandle.GetMessage(ex);
                }
            });
        }

        #region 页面拉伸相关

        public Visibility ComInfoVisibility
        {
            get { return (Visibility)GetValue(ComInfoVisibilityProperty); }
            set { SetValue(ComInfoVisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ComInfoVisibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ComInfoVisibilityProperty =
            DependencyProperty.Register("ComInfoVisibility", typeof(Visibility),
            typeof(ExhibitModelCtrl), new UIPropertyMetadata(Visibility.Visible));

        protected override void OnCommandMinSize(object obj)
        {
            //base.OnCommandMinSize(obj);
            if (ComInfoVisibility == Visibility.Visible)
            {
                ComInfoVisibility = Visibility.Collapsed;
            }
            else
            {
                ComInfoVisibility = Visibility.Visible;
            }
        }
        #endregion

        #region 页面可绑定数据

        /// <summary>
        /// 封面图片路径
        /// </summary>
        private string mMooMaPath = "";
        public string MooMaPath
        {
            get { return mMooMaPath; }
            set
            {
                if (value != mMooMaPath && MooMaPercent < 0)
                {
                    mMooMaPath = value;
                    MooMaPercent = 0;
                    PostTask(() =>
                    {
                        try
                        {
                            FileFactory.UploadFile(mMooMaPath, Model.ID.ToString(), FolderEnum.TEMP,
                            (int percent, int speed, long sended, long Length) =>
                            {
                                Dispatcher.Invoke(new Action(() =>
                                {
                                    MooMaPercent = percent / 100f;
                                }));
                            });
                            Dispatcher.Invoke(new Action(() =>
                            {
                                MooMaPercent = -1;
                                Model.MooMaUrl = Model.ID.ToString();
                                MessageStatus = MessageStatusEnum.Success;
                                Message = "封面上传完成";
                            }));
                        }
                        catch (System.Exception ex)
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                MooMaPercent = -1;
                                Model.MooMaUrl = "";
                                MessageStatus = MessageStatusEnum.Error;
                                Message = "封面上传失败：" + ExceptionHandle.GetMessage(ex);
                            }));
                        }
                    });
                }
            }
        }
        /// <summary>
        /// 展示类型
        /// </summary>
        private ExhibitType mExhibitType = null;
        public ExhibitType ExhibitType
        {
            get { return mExhibitType; }
            set
            {
                mExhibitType = value;
                PropertyNotifyChanged("ExhibitType");
                if (Model != null && value != null)
                {
                    Model.TypeID = value.ID;
                    Model.TypeName = value.Name;
                }
            }
        }
        /// <summary>
        /// 提供选择类型列表
        /// </summary>
        private IList<ExhibitType> mltExhibitType = null;
        public IList<ExhibitType> ExhibitTypes
        {
            get { return mltExhibitType; }
            set { mltExhibitType = value; PropertyNotifyChanged("ExhibitTypes"); }
        }

        #endregion

        #region 图片上传依赖属性
        /// <summary>
        /// 封面图片
        /// </summary>
        public string MooMaImage
        {
            get { return (string)GetValue(MooMaImageProperty); }
            set { SetValue(MooMaImageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MooMaImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MooMaImageProperty =
            DependencyProperty.Register("MooMaImage", typeof(string),
            typeof(ExhibitModelCtrl), new UIPropertyMetadata(""));

        public float MooMaPercent
        {
            get { return (float)GetValue(MooMaPercentProperty); }
            set { SetValue(MooMaPercentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MooMaPercent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MooMaPercentProperty =
            DependencyProperty.Register("MooMaPercent", typeof(float),
            typeof(ExhibitModelCtrl), new UIPropertyMetadata(-1.0f));

        #endregion
    }
}
